package com.njlime.cms.repository.sys;

import com.njlime.cms.entities.MaterialsNews;
import com.njlime.cms.entities.PublishReport;
import com.njlime.core.base.repository.impl.BaseDaoImpl;
import com.njlime.core.helper.PageBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Repository
public class MaterialNewsDaoImpl extends BaseDaoImpl<MaterialsNews>{


	public MaterialsNews get(int id, int unitId){
		StringBuffer sb = new StringBuffer();
		List<Object> arrList = new ArrayList<Object>();
		sb.append("select * from t_materials_news where id = ? and unit_id = ? and del = 0");
		arrList.add(id);
		arrList.add(unitId);
		return findOne(sb.toString(),arrList);
	}

    /**
     * 图片查询
     * @param pageBean
     * @return
     */
	public PageBean getNewsList(PageBean pageBean,  int unitId,int groupId,String name){
        StringBuffer sb = new StringBuffer();
        List<Object> arrList = new ArrayList<Object>();

        sb.append("select id, image_path1 ,image_path2 ,image_path3 ,title,abstract_info ");
		sb.append("from t_materials_news t  ");

		sb.append("where t.unit_id = ? and del = 0 ");
		arrList.add(unitId);
		if(groupId >0){
			sb.append(" and news_group_id = ? ");
			arrList.add(groupId);
		}
		if( StringUtils.isNotEmpty(name)){
			sb.append(" and t.title like ? ");
			arrList.add("%"+name+"%");
		}
		int num = getListJdbcSize(sb.toString(),arrList);
		pageBean.setTotalCount(num);
		if (num > 0) {
			sb.append(" order by id desc limit ?,?");
			arrList.add((pageBean.getCurPage() - 1) * pageBean.getPageSize());
			arrList.add(pageBean.getPageSize());
			pageBean.setMapList(getListJdbc(sb.toString(), arrList));
		}
		return pageBean;
	}


	public void update(MaterialsNews info){
		StringBuilder sb = new StringBuilder();
		List<Object> arrList = new ArrayList<Object>();
		sb.append("UPDATE t_materials_news set title = ?,image_path1= ?,");
		sb.append(" news_group_id = ?, image_size1 = ?,");
		sb.append(" abstract_info = ? , content = ?, ");
		sb.append(" image_path2 = ? , image_path3 = ?,");
		sb.append(" image_size2 = ? , image_size3 = ?");

		sb.append(" where id = ? and unit_id = ?");

		arrList.add(info.getTitle());
		arrList.add(info.getImage_path1());
		arrList.add(info.getNews_group_id());
		arrList.add(info.getImage_size1());
		arrList.add(info.getAbstract_info());
		arrList.add(info.getContent());
		arrList.add(info.getImage_path2());
		arrList.add(info.getImage_path3());
		arrList.add(info.getImage_size2());
		arrList.add(info.getImage_size3());

		arrList.add(info.getId());
		arrList.add(info.getUnit_id());
		updateJdbc(sb.toString(), arrList);
	}
	/**
	 *
	 *
	 * @param id
	 * @param unitId
	 * @return
	 */
	public void delNews(int id, int unitId){
		StringBuilder sb = new StringBuilder();
		List<Object> arrList = new ArrayList<Object>();
		sb.append("UPDATE t_materials_news set del = 1");

		sb.append(" where id = ? and unit_id = ?");
		arrList.add(id);
		arrList.add(unitId);
		updateJdbc(sb.toString(), arrList);
	}

	/**
	 * 图文分组
	 * @param id 图文id
	 * @param groupId 分组id
	 * @param unitId 单位id
	 */
	public void editNewsGroup(int id, int groupId, int unitId){
		StringBuilder sb = new StringBuilder();
		List<Object> arrList = new ArrayList<Object>();
		sb.append("UPDATE t_materials_news set news_group_id =   ?");
		sb.append(" where id= ?  and unit_id = ?");

		arrList.add(groupId);
		arrList.add(id);
		arrList.add(unitId);
		updateJdbc(sb.toString(), arrList);
	}


	public void editMediaId(Integer id, String mediaId,int unitId){
		StringBuilder sb = new StringBuilder();
		List<Object> arrList = new ArrayList<Object>();
		sb.append("UPDATE t_materials_news set media_id =   ?,media_time=?");
		sb.append(" where id= ?  and unit_id = ?");

		arrList.add(mediaId);
		arrList.add(new Date().getTime()/1000);
		arrList.add(id);
		arrList.add(unitId);
		updateJdbc(sb.toString(), arrList);
	}
}
